package com.songlanyun.modules.oss.controller.api;

import java.util.Arrays;
import java.util.Map;

import com.songlanyun.common.annotation.AuthIgnore;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.minio.entity.UploadResult;
import com.songlanyun.modules.oss.cloud.OSSFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.oss.entity.SysOssEntity;
import com.songlanyun.modules.oss.service.SysOssService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 文件上传
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-23 15:35:49
 */
@RestController
@RequestMapping("api/sys/oss")
public class SysOssApiController extends AbstractApiController {
    @Autowired
    private SysOssService sysOssService;

    @Autowired
    private AccountService accountService;


    @PostMapping("/upload")
    public R upload(@RequestParam("file") MultipartFile file) throws Exception {

        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
        String uploadFile = url.substring(url.lastIndexOf("/") + 1);
        //保存文件信息
//        SysOssEntity ossEntity = new SysOssEntity();
//        ossEntity.setUrl(url);
//        sysOssService.save(ossEntity);

        return R.ok().put("data", UploadResult.builder().url(url).filename(uploadFile).build());
    }


    /**
     * 保存
     */
    @PostMapping("/save/{shopId}")
//    @RequiresPermissions("oss:sysoss:save")
    public R save(@PathVariable("shopId") Long shopId, @RequestBody SysOssEntity sysOss) {
        ValidatorUtils.validateEntity(sysOss);
        sysOss.setShopId(shopId);
        sysOssService.save(sysOss);

        return R.ok();
    }


    /**
     * 二维码
     *
     * @param
     */
    @GetMapping("/qrcode")
    public R qrcode(@RequestParam("path") String path) {
        Long userId = getUserId(false);
        String url = "";
        if (userId != 0) {
            String code = accountService.getCodeById(userId);
            url = sysOssService.saveQRcode(path, code);
        }
        return R.ok().put("data", url);
    }

    /**
     * 小程序二维码
     *
     * @param path 最大32个可见字符，只支持数字，大小写英文以及部分特殊字符：!#$&'()*+,/:;=?@-._~，
     *             其它字符请自行编码为合法字符（因不支持%，中文无法使用 urlencode 处理，请使用其他编码方式）
     * @return
     */
    @GetMapping("/wxcode")
    public R wxcode(@RequestParam("path") String path) {
        String code = accountService.getCodeById(getUserId(false));

        String url = sysOssService.saveWXcode(path, code);
        return R.ok().put("data", url);
    }

}
